﻿using Cofe.Core;
using Cofe.Core.Implements;
using Cofe.Core.Interfaces;
using Cofe.Core.Logger;
using Cofe.Core.Property;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cofe.IO.Implements
{
#if !NETFX_CORE
    public class SystemIODirectoryLister : IDirectoryLister
    {
        #region Constructor
        public SystemIODirectoryLister()
        {
            ManualEventSource = new DummyCofeSystemEventSource();
        }

        #endregion

        #region Methods

        public Task<IPropertyProvider> TryParsePathAsync(string path, EntryConstructionFlags flags)
        {            
            string fullPath = PathFE.Combine(VolumePath, path);

            IPropertyProvider parsedEntryCore = null;
            if (System.IO.Directory.Exists(fullPath))
                parsedEntryCore = new SystemIOPropertyProvider(this,
                    new System.IO.DirectoryInfo(fullPath)) { ManualEventSource = ManualEventSource };
            else if (System.IO.File.Exists(fullPath))
                parsedEntryCore = new SystemIOPropertyProvider(this,
                    new System.IO.FileInfo(fullPath)) { ManualEventSource = ManualEventSource };

            if (parsedEntryCore != null)
                CofeServices.Logger.Log("SystemIO parsed {0}", path);
            else CofeServices.Logger.Log("SystemIO parsed failed {0}", path);

            return Task.FromResult<IPropertyProvider>(parsedEntryCore);
        }


        public IEnumerable<IPropertyProvider> GetRootPropertyProviders()
        {
            yield return new SystemIOPropertyProvider(this,
                             new System.IO.DirectoryInfo(VolumePath)) { ManualEventSource = ManualEventSource };
        }

        #endregion

        #region Data
        
        private IVolume _volume;

        #endregion

        #region Public Properties

        internal IManualCofeSystemEventSource ManualEventSource { get; set; }

        public string VolumePath
        {
            get { return Volume.Parameters["VolumePath"]; } 
        }

        public IVolume Volume
        {
            get { return _volume; }
            set { _volume = value; }            
        }

        #endregion




    }
#endif

}
